package com.qf.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.qf.model.entity.Department;
import com.qf.service.DepartmentService;

import java.util.ArrayList;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author TRC
 * @date 2024/5/4
 * @desc
 */
public class DepartmentListener extends AnalysisEventListener<Department> {

    private List<Department> departmentList = new ArrayList<>();

    private static final int BATCH_COUNT = 5;

    private DepartmentService departmentService;

    public DepartmentListener() {}

    public DepartmentListener(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @Override
    public void invoke(Department department, AnalysisContext analysisContext) {
        departmentList.add(department);
        if (departmentList.size() >= BATCH_COUNT) {
            departmentService.insertBatch(departmentList);
            departmentList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (departmentList.size() > 0) {
            departmentService.insertBatch(departmentList);
            departmentList.clear();
        }
    }
}
